package com.i_art_engineer.meigong.widget;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.i_art_engineer.meigong.util.DeviceUtils;
import com.i_art_engineer.meigong.util.DimenUtils;

/**
 * 自制ActionBar 兼容老系统
 */
public class ActionBar extends LinearLayout {

    private Context mContext;//上下文
    private LinearLayout linearLayout;//中间按钮部分的布局
    private ImageView leftButton;//title左边的按钮
    private View noticeView;//顶部透明栏
    private TextView titleView;//title,可从外部设置其content，默认显示APP名称
    private int leftPadding;//中间部分按钮图标的padding，用以规整外观
    private int barHeight; //默认高度
    public static final int DEFAULT_ACTIONBAR_COLOR = Color.parseColor("#000000");

    public static boolean isSupportImmersive = false;

    public ActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        int linePadding = DimenUtils.dp2px(mContext, 2);
        int statusHeight = DeviceUtils.getStatusHeight(((Activity) mContext));
        barHeight = DeviceUtils.getActionBarHeight(mContext);
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, barHeight + statusHeight + 5));
        setOrientation(VERTICAL);
        if (isSupportImmersive) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = ((Activity) context).getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);

                noticeView = new View(mContext);
                noticeView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusHeight));
                addView(noticeView);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

                noticeView = new View(mContext);
                noticeView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusHeight));
                noticeView.setBackgroundColor(DEFAULT_ACTIONBAR_COLOR);
                addView(noticeView);
            }
        }

        linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, barHeight));
        linearLayout.setPadding(linePadding, linePadding, linePadding, linePadding);
        linearLayout.setOrientation(HORIZONTAL);
        linearLayout.setBackgroundColor(DEFAULT_ACTIONBAR_COLOR);
        addView(linearLayout);

        leftButton = new ImageView(context);
        leftButton.setClickable(true);
        leftButton.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        leftButton.setTop(leftButton.getHeight());
        leftPadding = DimenUtils.dp2px(mContext, 16);
        int padding = DimenUtils.dp2px(mContext, 10);
        leftButton.setPadding(leftPadding, padding, padding, padding);
        linearLayout.addView(leftButton);

        titleView = new TextView(mContext);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        lp.rightMargin=leftPadding*2;
        titleView.setLayoutParams(lp);
        titleView.setGravity(Gravity.CENTER);
        titleView.setTextColor(Color.WHITE);
        //titleView.setPadding(0, 0, dp2px(56), 0);
        titleView.setTextSize(20);
        titleView.setText(getApplicationName());
        linearLayout.addView(titleView);

//        View gradientView = new View(mContext);
//        gradientView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 5));
//        gradientView.setBackgroundResource(R.mipmap.actionbar_bottom_gradient);
//        addView(gradientView);
    }

    /**
     * @param resImg   图标
     * @param listener 按钮响应事件
     */
    public void setLeftButton(int resImg, OnClickListener listener) {
        leftButton.setImageResource(resImg);
        leftButton.setOnClickListener(listener);
    }

    /**
     * @param listener 按钮响应事件
     */
    public void setLeftButton(OnClickListener listener) {
        leftButton.setOnClickListener(listener);
    }

    /**
     * @param color    actionbar的背景色
     * @param resImg   图标
     * @param listener 按钮响应事件
     */
    public void setRightView(int color, final int[] resImg, final OnItemClickListener listener) {
        linearLayout.setBackgroundColor(color);
        setRightView(resImg, listener);
    }

    public void setRightView(final CharSequence text, final int[] resImg, final OnItemClickListener listener) {
        titleView.setText(text);
        setRightView(resImg, listener);
    }


    public void setRightView(final int[] resImg, final OnItemClickListener listener) {
        LayoutParams lp = new LayoutParams(barHeight, barHeight);
        if (resImg != null)
            for (int i = 0; i < resImg.length; i++) {
                ImageView imageView = new ImageView(mContext);
                imageView.setClickable(true);
                imageView.setPadding(leftPadding, leftPadding, leftPadding, leftPadding);
                imageView.setImageResource(resImg[i]);
                final int finalI = i;
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onItemClick(v, resImg[finalI]);
                        }
                    }
                });
                imageView.setLayoutParams(lp);
                linearLayout.addView(imageView);
            }
    }

    /**
     * @param color actionbar的背景色
     */
    public void setRightView(int color) {
        this.setRightView(color, null, null);
    }

    /**
     * @param color 通知栏的背景色
     */
    public void setNoticeView(int color) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
            return;
        noticeView.setBackgroundColor(color);
    }

    /**
     * @param text 手动设置title的内容
     */
    public void setTitleView(CharSequence text) {
        titleView.setText(text);
    }

    /*以下为接口*/

    public interface OnItemClickListener {
        void onItemClick(View view, int resImg);
    }

    /*
    *获取APP名称
    */
    public String getApplicationName() {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = mContext.getApplicationContext().getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(mContext.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

}
